package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.TranformationExecutionLayoutPolicy;
import edu.pku.sei.transformation.editor.internal.TransformationEditorPaletteFactory;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.execution.AtomicTransformationExecution;
import edu.pku.sei.transformation.editor.views.figures.TransformationExecutionFigure;

public class AtomicTransformationExecutionEditpart extends
		TransformationExecutionEditpart {

	public AtomicTransformationExecutionEditpart(MetaShapeElement model) {
		super(model);
	}
	
	@Override
	protected IFigure createFigure() {
		TransformationExecutionFigure figure =  new TransformationExecutionFigure();
		AtomicTransformationExecution te = (AtomicTransformationExecution) getNode().getModelElement();
		figure.setName(te.toString());
		figure.setDescription(te.IsSpcifiedRules());
		if (getNode().getDiagram().getType().equals(TransformationEditorPaletteFactory.TRANSFORMATION_TEST)) {
			figure.setTag("<<Transformation Test>>");
		} else if (getNode().getDiagram().getType().equals(TransformationEditorPaletteFactory.TRANSFORMATION_COMPOSITE_DIAGRAM)
				|| getNode().getDiagram().getType().equals(TransformationEditorPaletteFactory.TRANSFORMATION_EXECUTION)) {
			figure.setTag("<<Transformation Execution>>");
		} else {
			figure.setTag("");
		}
		return figure;
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		TransformationExecutionFigure figure = (TransformationExecutionFigure) getFigure();
		AtomicTransformationExecution execution = (AtomicTransformationExecution) getNode().getModelElement();
		if (ModelPackage.TRANSEXE_SPECIFIED_RULES.equals(propertyName)) {
			figure.setDescription(execution.IsSpcifiedRules());
		} else {
			super.handlePropertyChanged(event);
		}
	}
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new TranformationExecutionLayoutPolicy());
	}
	
	@Override
	public IFigure getContentPane() {
		TransformationExecutionFigure figure = (TransformationExecutionFigure) getFigure();
		return figure.getBody();
	}
}
